/*
 * Copyright (c) 2010 - 2011, Brookhaven National Laboratory
 * All rights reserved.
 * The software in this package is published under the terms
 * of the BSD license a copy of which has been included with
 * this distribution in the license.txt file.
 */

package org.oxtypes.impl.modifiable;

import java.util.Map;

import org.omg.dds.type.dynamic.DynamicType;
import org.omg.dds.type.dynamic.AnnotationDescriptor;
import org.omg.dds.type.dynamic.modifiable.ModifiableAnnotationDescriptor;

import org.oxtypes.impl.DDSObjectImpl;

/**
 *
 */
public class ModifiableAnnotationDescriptorImpl extends DDSObjectImpl
        implements ModifiableAnnotationDescriptor {

    // --- From Value: ---------------------------------------------------

    public ModifiableAnnotationDescriptor modify(){
        modifiable  = true;
        return this;
    }

    // --- From ModifiableValue: ------------------------------------

    public ModifiableAnnotationDescriptor copyFrom(AnnotationDescriptor other){
            body.type = other.getType();
            // body.values = other.getAllValue(null) ?
            return this;
    }

    public AnnotationDescriptor finishModification(){
        modifiable  = false;
        return body.clone();
    }

    public ModifiableAnnotationDescriptor clone(){
        throw new java.lang.UnsupportedOperationException();
    }

    // --- From AnnotationDescriptor: ------------------------------------

    public String getValue(String key){
        return body.getValue(key);
    }

    public Map<String, String> getAllValue(Map<String, String> value){
        throw new java.lang.UnsupportedOperationException(); // ?
    }

    public boolean isConsistent(){
        throw new java.lang.UnsupportedOperationException();
    }

    public DynamicType getType() {
        return body.getType();
    }

    // --- From ModifiableAnnotationDescriptor: ------------------------------

    public int setValue(String key, String value){
        if(modifiable) body.values.put(key, value);
        return 0; // ?
    }

    public ModifiableAnnotationDescriptor setType(DynamicType type){
        if(modifiable) body.type = type;
        return this;
    }

    //
   
    private AnnotationDescriptorImpl body;
    private boolean modifiable = true;
    
    private class AnnotationDescriptorImpl extends DDSObjectImpl
            implements AnnotationDescriptor {
        
        // --- From Value: ---------------------------------------------------

        @Override
        public AnnotationDescriptor clone(){
            AnnotationDescriptorImpl result = new AnnotationDescriptorImpl();
            result.type = type;
            if(values != null) {
                result.values = new java.util.TreeMap<String, String>(values);
            }
            return result;
        }

        public ModifiableAnnotationDescriptor modify(){
            return null;
        }

        // --- From AnnotationDescriptor: ------------------------------------
        
        public String getValue(String key){
            return values.get(key);
        }

        public Map<String, String> getAllValue(Map<String, String> value){
            throw new java.lang.UnsupportedOperationException();
        }

        public boolean isConsistent(){
            throw new java.lang.UnsupportedOperationException();
        }

        public DynamicType getType() {
            return type;
        }
        
        private DynamicType type;
        private Map<String, String> values;
        
    }



}
